/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse;

import java.util.*;
import java.sql.*;

/*********
 * This class is run by NetFSEServer and starts threads to monitor datafiles for
 * new records. It runs indefinitely, periodically checking the state of the
 * app_list database table to find new entries.
 * 
 * Does not currently handle the case where a DataFileMonitor is removed. Only a
 * system restart will correct this.
 * 
 * @author bduphoff
 * 
 */

public class DataFileMonitorLauncher extends Thread {

	private Vector<DataFileMonitor> dataFileMonitors = new Vector<DataFileMonitor>();

	/******************
	 * Runs forever, periodically checks to see if new configuration information
	 * has been added. If so, starts new DataFileMonitor.
	 */
	public void run() {
		try {
			MetaDatabase mdb = MetaDatabase.create();
			while (true) {
				Statement s = mdb.createStatement();

				// Create a list of appIDs based on the app_list table
				Vector<Integer> appIDs = new Vector<Integer>();
				String sql = "select * from app_list";
				ResultSet rs = s.executeQuery(sql);
				while (rs.next()) {
					appIDs.addElement(rs.getInt("App_ID"));
				}
				rs.close();

				// For each sensor, find DataFileMonitors and start them
				sql = "select * from sensors";
				rs = s.executeQuery(sql);
				while (rs.next()) {
					int sensorID = rs.getInt("ID");
					for (int x = 0; x < appIDs.size(); x++) {
						int appID = appIDs.elementAt(x);
						boolean found = false;
						for (int i = 0; i < dataFileMonitors.size(); i++) {
							DataFileMonitor mon = dataFileMonitors.elementAt(i);
							if (mon.getSensorID() == sensorID
									&& mon.getAppID() == appID) {
								found = true;
								break;
							}
						}
						if (!found) {
							System.out
									.println("Starting Data File Monitor for sensor "
											+ sensorID + ", app " + appID);
							DataFileMonitor monitor = new DataFileMonitor(
									sensorID, appID);
							dataFileMonitors.addElement(monitor);
							monitor.start();
						}
					}
				}
				rs.close();
				s.close();

				// Sleep for a minute
				Thread.sleep(60 * 1000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}